<template>
  <div class="search">
    <el-input
      id="headerInput"
      @click.native="onClickInput"
      @keypress.native.enter="onEnterPress"
      placeholder="搜索"
      prefix-icon="el-icon-search"
      ref="input"
      v-model.trim="searchKeyword"
      class="inputStyle"
    ></el-input>

    <div id="searchPanel" class="search-panel" v-show="searchPanelShow">
      <div class="search-hots">
        <div class="block">
          <p class="title">热门搜索</p>
          <div class="tags">
            <span :key="index" @click="onClickHot(hot.first)" class="button" v-for="(hot, index) in searchHots">{{ hot.first }}</span>
          </div>
        </div>
        <div class="block">
          <p class="title">搜索历史</p>
          <div class="tags" v-if="searchHistorys.length">
            <span :key="index" @click="onClickHot(history)" class="button" v-for="(history, index) in searchHistorys">{{ history.first }}</span>
          </div>
          <div class="empty" v-else>暂无搜索历史</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import storage from 'good-storage'
import { getSearchHot } from 'network/request'

const SEARCH_HISTORY_KEY = '__search_history__'
export default {
  async created() {
    const { result } = await getSearchHot()
    this.searchHots = result.hots
  },
  data() {
    return {
      searchPanelShow: false,
      searchKeyword: '',
      searchHots: [],
      searchHistorys: storage.get(SEARCH_HISTORY_KEY, []),
      reserveDoms: []
    }
  },
  methods: {
    goSearch(keywords) {
      // 删除相同元素
      this.searchHistorys = this.searchHistorys.filter(x => x.first != keywords)
      this.searchHistorys.unshift({ first: keywords })
      storage.set(SEARCH_HISTORY_KEY, this.searchHistorys)
      this.$router.push(`/result/${keywords}`)
      this.searchPanelShow = false
    },
    onClickInput() {
      this.searchPanelShow = true
    },
    onEnterPress() {
      if (this.searchKeyword) {
        this.goSearch(this.searchKeyword)
      }
    },
    onClickHot(hot) {
      if (typeof hot == 'string') {
        this.goSearch(hot)
      } else {
        this.goSearch(hot.first)
      }
    },
    clickEvent(e) {
      if (document.getElementById('headerInput') == e.target) {
        return false
      }
      if (!document.getElementById('searchPanel').contains(e.target)) {
        this.searchPanelShow = false
      }
    },
    bindClick() {
      document.addEventListener('mousedown', this.clickEvent)
    },
    removeClick() {
      document.removeEventListener('mousedown', this.clickEvent)
    }
  },
  watch: {
    searchPanelShow(val) {
      setTimeout(() => {
        if (val) {
          this.bindClick()
        } else {
          this.removeClick()
        }
      }, 0)
    }
  },
  beforeDestroy() {
    this.removeClick()
  }
}
</script>

<style lang="scss">
/deep/.el-input {
  .el-input__inner {
    background: var(--header-input-bgcolor) !important;
    border: none !important;
    color: var(--header-input-color) !important;
  }
}
</style>
<style lang="scss" scoped>
.search {
  position: relative;
  width: 150px;

  .search-panel {
    position: fixed;
    top: $header-height;
    bottom: $mini-player-height;
    right: 0;
    width: 350px;
    background: var(--search-bgcolor);
    z-index: $search-panel-z-index;
    font-size: $font-size-sm;
    overflow-y: auto;
    @include box-shadow;

    .block {
      padding: 16px 24px;

      .title {
        color: var(--font-color-grey);
        margin-bottom: 16px;
      }
      .tags {
        display: flex;
        flex-wrap: wrap;

        .button {
          height: 20px;
          padding: 2px 16px 22px 16px;
          line-height: 20px;
          margin-bottom: 12px;
          margin-right: 6px;
          border: 1px solid var(--button-border-color);
          font-size: $font-size-sm;
          &:hover {
            cursor: pointer;
            background-color: var(--button-hover-bgcolor);
          }
        }
      }

      .empty {
        @include flex-center();
        color: var(--font-color-grey);
        font-size: $font-size;
        height: 100px;
      }
    }
  }
}
</style>
